#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/wait.h>
#include<stdlib.h>
#include<string.h>
//输出型参数
//管道只能单向通信
 
//child->write , father->read 
int main()
{
 int fd[2]={0}; //fd[0],fd[1] 读端，写端
 if(pipe(fd)<0)
 {
   perror("pipe!");
   return 1;
 }
 
 pid_t id=fork();
 //child
 if(id==0)
 {
  close(fd[0]);
  const char* str="hello father, I am child!";
  int count=10;
  while(count--)
  {
  write(fd[1],str,strlen(str));
  sleep(1);
  }
 exit(0);
 }

 //father
 close(fd[1]);  
 char buff[64]; 
 while(1)
 {

  ssize_t s= read(fd[0],buff,sizeof(buff));
  if(s>0)
  {
    buff[s]='\0';
    printf("child send to father## %s\n",buff);
  }
  else if(s==0)
  {
    printf("read file end!!\n");
    break;
  }
  else 
  {
    printf("read erro\n");
  }
 }
 waitpid(id,NULL,0);

  return 0;
}
